package data;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;

import org.restlet.resource.ClientResource;

import restlet.interfaces.TripPartResourceI;
import beans.TripPartBean;

public class AddTripsAndCarriers{

	public static void main(String[] args) {

		//final String URL = "localhost:8888"; 
		final String URL = "idemdo.appspot.com"; 

		ClientResource cr = new ClientResource("http://" + URL + "/rest/tripParts");
		TripPartResourceI tripPartResource = cr.wrap(TripPartResourceI.class);

		System.out.println("Uspesno inicijalozovan Consumer");

		System.out.println("Startovano...");

		System.out.println("Prikupljamo podatke");

		//GSPNS gspNS = new GSPNS();
		//List<TripPartBean> tripParts = gspNS.procitajPodatke();


		/*try {
		FileOutputStream fout = new FileOutputStream("tripParts.dat");
		  ObjectOutputStream oos = new ObjectOutputStream(fout);
		  oos.writeObject(tripParts);
		  oos.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}*/

		List<TripPartBean> tripParts = null;

		try {
			FileInputStream fin = new FileInputStream("tripParts.dat");
			ObjectInputStream ois = new ObjectInputStream(fin);
			tripParts = (List<TripPartBean>) ois.readObject();
			ois.close();
		}
		catch (Exception e) { e.printStackTrace(); }


		/*MestoBean m1 = new MestoBean("Novi Sad");
	MestoBean m2 = new MestoBean("Beograd");

	CarrierBean c1 = new CarrierBean("lasta");
	c1.setVrstaPrevoza(new TripCatBean("Bus"));

	Date d1 = Calendar.getInstance().getTime();

	TripPartBean tpb = new TripPartBean(0, 50, d1,d1, new ArrayList<BeneffitBean>(),
			m1, m2, c1);

	List<TripPartBean> tripParts = new ArrayList<TripPartBean>();

	tripParts.add(tpb);
	tripParts.add(tpb);
	tripParts.add(tpb);
	tripParts.add(tpb);*/

		System.out.println("Ucitano: " + tripParts.size() + "putnih duzi!");

		System.out.println("Saljemo podatke");

		int i =0; int j =0;
		for(TripPartBean tp : tripParts){
			System.out.println("(" + i++ + ") Saljemo: " + tp);

			try {
				tripPartResource.addTripPart(tp);
				//sleep(500);
			} catch (Exception e) {
				j++;
				e.printStackTrace();
			}

		}
		System.out.println("Ukupno gresaka: " + j);
	}


}
